package com.store.practice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.store.practice.mapper.BgAppointmentOrderMapper;
import com.store.practice.model.BgAppointmentOrder;
import com.store.practice.model.BgAppointmentOrderExample;
import com.store.practice.service.StoreOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StoreOrderServiceImpl implements StoreOrderService {
    @Autowired
    private BgAppointmentOrderMapper orderMapper;

    @Override
    public List<Map> getOrder(Integer storeId) {
        ArrayList<Map> result = new ArrayList<>();
        BgAppointmentOrderExample example = new BgAppointmentOrderExample();
        example.createCriteria().andStoreIdEqualTo(storeId);
        List<BgAppointmentOrder> orders = orderMapper.selectByExample(example);
        Map<String, Object> map = new HashMap<>();
        if (orders==null){
            return null;
        }
        for (BgAppointmentOrder order:orders){
            map = BeanUtil.beanToMap(order);
            switch (order.getStatus()){
                case 0:
                    map.put("status","已取消");
                    break;
                case 1:
                    map.put("status","已预约");
                    break;
                case 2:
                    map.put("status","已完成");
                    break;
            }
            map.put("serviceDate", DateUtil.format(order.getServiceDate(),"yyyy-MM-dd"));
            map.put("startTime",DateUtil.format(order.getStartTime(),"HH:mm:SS"));
            map.put("endTime",DateUtil.format(order.getEndTime(),"HH:mm:SS"));
            result.add(map);
        }
        return result;
    }

    @Override
    public int update(Integer id, Integer param) {
        BgAppointmentOrder order = orderMapper.selectByPrimaryKey(id);
        if (param==0){
            order.setStatus(0);
        }
        if (param==1){
            order.setStatus(2);
        }
        return orderMapper.updateByPrimaryKey(order);
    }
}
